/* Octopus PDF - A simple document viewer for Android.
   Copyright (C) 2013  Gerald Thaler

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
package de.geraldthaler.octopuspdf.view;

import android.view.MotionEvent;

/**
 * @author Gerald Thaler
 */
public final class Touch {
    public static Touch make(final MotionEvent event) {
        final int action = event.getActionMasked();
        final int actionIndex;
        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            actionIndex = 0;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_POINTER_UP:
            actionIndex = event.getActionIndex();
            break;
        case MotionEvent.ACTION_MOVE:
            actionIndex = -1;
            break;
        default:
            return null;
        }
        final int pointerCount = event.getPointerCount();
        if (pointerCount == 0)
            return null;
        final int[] pointerIds = new int[pointerCount];
        final float[] x = new float[pointerCount];
        final float[] y = new float[pointerCount];
        for (int n = 0; n < pointerCount; ++n) {
            pointerIds[n] = event.getPointerId(n);
            x[n] = event.getX(n);
            y[n] = event.getY(n);
        }
        return new Touch(action, actionIndex, pointerIds, x, y);
    }

    private final int action;
    private final int actionIndex;
    private final int[] pointerIds;
    private final float[] x;
    private final float[] y;
    
    private Touch(final int action, final int actionIndex, final int[] pointerIds, final float[] x, final float[] y) {
        this.action = action;
        this.actionIndex = actionIndex;
        this.pointerIds = pointerIds;
        this.x = x;
        this.y = y;
    }

    public int getAction() {
        return action;
    }

    public int getActionIndex() {
        return actionIndex;
    }

    public int[] getPointerIds() {
        return pointerIds;
    }

    public float[] getX() {
        return x;
    }

    public float[] getY() {
        return y;
    }
}
